/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/
 
/**
*\*\file n32g430_it.c
*\*\author Nations
*\*\version v1.0.0
*\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
**/

#include "n32g430_it.h"
#include "n32g430.h"
#include "main.h"
#include "bsp_led.h"
#include "usart.h"
#include "key_input.h"
#include "bsp_delay.h"
#include "update.h"
#include "i2c.h"
/******************************************************************************/
/*            Cortex-M4 Processor Exceptions Handlers                         */
/******************************************************************************/

static __IO uint32_t I2CTimeout;
/**
 *\*\name   NMI_Handler.
 *\*\fun    This function handles NMI exception.
 *\*\param  none.
 *\*\return none.
 */
void NMI_Handler(void)
{
}


/**
 *\*\name   HardFault_Handler.
 *\*\fun    This function handles Hard Fault exception.
 *\*\param  none.
 *\*\return none.
 */
void HardFault_Handler(void)
{
    /* Go to infinite loop when Hard Fault exception occurs */
    while (1)
    {
    }
}


/**
 *\*\name   MemManage_Handler.
 *\*\fun    This function handles Memory Manage exception.
 *\*\param  none.
 *\*\return none.
 */
void MemManage_Handler(void)
{
    /* Go to infinite loop when Memory Manage exception occurs */
    while (1)
    {
    }
}


/**
 *\*\name   BusFault_Handler.
 *\*\fun    This function handles Bus Fault exception.
 *\*\param  none.
 *\*\return none.
 */
void BusFault_Handler(void)
{
    /* Go to infinite loop when Bus Fault exception occurs */
    while (1)
    {
    }
}


/**
 *\*\name   UsageFault_Handler.
 *\*\fun    This function handles Usage Fault exception.
 *\*\param  none.
 *\*\return none.
 */
void UsageFault_Handler(void)
{
    /* Go to infinite loop when Usage Fault exception occurs */
    while (1)
    {
    }
}


/**
 *\*\name   SVC_Handler.
 *\*\fun    This function handles SVCall exception.
 *\*\param  none.
 *\*\return none.
 */
void SVC_Handler(void)
{
}


/**
 *\*\name   DebugMon_Handler.
 *\*\fun    This function handles Debug Monitor exception.
 *\*\param  none.
 *\*\return none.
 */
void DebugMon_Handler(void)
{
}


/**
 *\*\name   SysTick_Handler.
 *\*\fun    This function handles SysTick Handler.
 *\*\param  none.
 *\*\return none.
 */
void SysTick_Handler(void)
{
}


/******************************************************************************/
/*                 N32G430 Peripherals Interrupt Handlers                     */
/*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
/*  available peripheral interrupt handler's name please refer to the startup */
/*  file (startup_n32g430.s).                                                 */
/******************************************************************************/

void TIM1_UP_IRQHandler(void)
{
    if (TIM_Interrupt_Status_Get(TIM1, TIM_INT_UPDATE) != RESET)
    {
        TIM_Interrupt_Status_Clear(TIM1, TIM_INT_UPDATE);

        /* Pin PC.06 toggling */
        //GPIO_Pin_Toggle(LED2_GPIO_PORT, LED1_GPIO_PIN);
    }
}

int led_cnt = 0;
void TIM6_IRQHandler(void)
{
    if (TIM_Interrupt_Status_Get(TIM6, TIM_INT_UPDATE) != RESET)
    {
        TIM_Interrupt_Status_Clear(TIM6, TIM_INT_UPDATE);

        if (led_freq == LED_TOGGLE_100_MS)
        {
            GPIO_Pin_Toggle(LED2_GPIO_PORT, LED1_GPIO_PIN);
            led_cnt = 0;
        }
        else if (led_freq == LED_TOGGLE_1000_MS)
        {
            if (led_cnt == LED_TOGGLE_1000_MS)
            {
                GPIO_Pin_Toggle(LED2_GPIO_PORT, LED1_GPIO_PIN);
                led_cnt = 0;
            }
        }

        led_cnt++;
    }
}

void TIM2_IRQHandler(void)
{
    if (TIM_Interrupt_Status_Get(TIM2, TIM_INT_UPDATE) != RESET)
    {
        TIM_Interrupt_Status_Clear(TIM2, TIM_INT_UPDATE);     
    }
}

/**
*\*\name    USARTz_IRQHandler.
*\*\fun     This function handles USARTz global interrupt request.
*\*\param   none
*\*\return  none 
**/
static uint8_t RxBuffer1[128];//接收数据缓存区
static uint32_t RxCounter1 = 0;//接收数据长度

void USART2_IRQHandler(void)
{
    if (USART_Interrupt_Status_Get(USART2, USART_INT_RXDNE) != RESET)
    {
        /* 读取数据并清除标志 */
        USART_Interrupt_Status_Clear(USART2,USART_INT_RXDNE);
        RxBuffer1[RxCounter1++] = USART_Data_Receive(USART2);
    }
    else if (USART_Interrupt_Status_Get(USART2, USART_INT_IDLEF) != RESET)
    {
        /* 清除IDLE标志*/
        USART_Interrupt_Status_Clear(USART2,USART_INT_IDLEF);
        
        if (RxCounter1 > 0)
        {
            handle_uart_message(RxBuffer1,RxCounter1);
            RxCounter1 = 0;
        }
    }
}

char *note2 = "in EXTI4_IRQHandler \r\n";

// exti 5 - 9 line handler fuc
// 跳转到Applition执行
// 做一个消抖处理
void EXTI9_5_IRQHandler(void)
{
    if (RESET != EXTI_Interrupt_Status_Get(KEY2_INPUT_EXTI_LINE))
    {
        SysTick_Delay_Ms(10);
        if (GPIO_Input_Pin_Data_Get(KEY2_INPUT_PORT,KEY2_INPUT_PIN) == RESET)
        {
            EXTI_Interrupt_Status_Clear(KEY2_INPUT_EXTI_LINE); 
            Max_Info("EXTI9_5_IRQHandler \r\n");      
        }
    }
}

// key1 pa4 irq function
void EXTI4_IRQHandler(void)
{
    if (RESET != EXTI_Interrupt_Status_Get(KEY1_INPUT_EXTI_LINE))
    {
        EXTI_Interrupt_Status_Clear(KEY1_INPUT_EXTI_LINE);
        Max_Info("EXTI4_IRQHandler \r\n");      

    }
}

/**
 * i2c 1 slave interrupt
*/

#if I2C1_MODE == I2C1_SLAVE
void I2C1_EV_IRQHandler(void)
{
    uint8_t timeout_flag = 0;
    uint32_t last_event = 0;
    
    
    last_event = I2C_Last_Event_Get(I2C1);

    if ((last_event & I2C_ROLE_MASTER) != I2C_ROLE_MASTER) /* MSMODE = 0:I2C slave mode */
    {
        switch (last_event)
        {
        case I2C_EVT_SLAVE_RECV_ADDR_MATCHED: /*0x00020002.EV1 Rx addr matched */
            /* clear flag,ready to receive data */
            rxDataNum = 0;
            break;
        case I2C_EVT_SLAVE_SEND_ADDR_MATCHED: /*0x00060082.EV1 Tx addr matched */
            send_i2c_message(rxData,rxDataNum);
            txDataNum = 0;
            I2C1->DAT = txData[txDataNum++];/* Send first data */
            break;
        /* SlaveTransmitter	 */
        case I2C_EVT_SLAVE_DATA_SENDING:  /*0x00060080. EV3 Sending data */
            //printf("I2C_EVT_SLAVE_DATA_SENDING\r\n");
            break;
        case I2C_EVT_SLAVE_DATA_SENDED:
            I2C1->DAT = txData[txDataNum++];
            break;
        /* SlaveReceiver */
        case I2C_EVT_SLAVE_DATA_RECVD: /*0x00020040.EV2 one byte recved */
            rxData[rxDataNum++] = I2C1->DAT;
            break;
        case I2C_EVT_SLAVE_STOP_RECVD: /* 0x00000010 EV4 */
            I2C_ON(I2C1);   
            handle_i2c_message(rxData,rxDataNum);
            break;
        default:
            I2C_ON(I2C1);
            timeout_flag = 1;
            break;
        }
    }
    
    if (timeout_flag)
    {
        if ((I2CTimeout--) == 0)
        {
            //CommTimeOut_CallBack(SLAVE_UNKNOW);
        }
    }
    else
    {
        I2CTimeout = I2CT_LONG_TIMEOUT;
    }
       
}


void I2C1_ER_IRQHandler(void)
{
    uint32_t last_event;
    last_event = I2C_Last_Event_Get(I2C1);
    if(last_event == I2C_EVT_SLAVE_ACK_MISS)   
    {   
        I2C_Flag_Status_Clear(I2C1, I2C_FLAG_ACKFAIL);
        if(rxDataNum != 0)  /*slave send the last data and recv NACK  */
        {
            //flag_slave_send_finish = 1;
        }
        else /*not the last data recv nack, send fail */
        {
        }
    }
}

#endif

